package com.atugigu.demo.customer.controller;

import com.atugigu.demo.customer.bean.Customer;
import com.atugigu.demo.customer.service.CustomerService;
import com.atugigu.demo.customer.service.impl.CustomerServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController  //标识控制类
public class CustomerController {

    @Autowired
    CustomerService customerService;

    @RequestMapping("/hello")
    public  String sayHello(@RequestParam(value = "name",required = false,defaultValue ="zc" ) String name ,@RequestParam(value ="age",required = false, defaultValue ="99") Integer age ){

        String abc="";
        System.out.println("hello world :"+name + ", age :"+age+abc.toString()) ;


        return "hello world :"+name + ", age :"+age;
    }

    @RequestMapping("/customer/query")
    public  List<Customer> queryCustomer(@RequestParam(value = "name",required = false  ) String name ,@RequestParam(value ="age",required = false, defaultValue ="-1") Integer age ){
        QueryWrapper<Customer> query = new QueryWrapper<Customer>();
        if(name!=null){
            query.eq("name",name);
        }
        if(age!= -1){
            query.eq("age",age);
        }
        List<Customer> customerList = customerService.list(query);


        return customerList;
    }

    @RequestMapping(value = "/customer/{id}.html" ,method = RequestMethod.GET)
    @GetMapping("/customer/{id}.html")
    public String getCustomer( @PathVariable("id") String id){
        System.out.println("customer id  :"+id);
        Customer customer= customerService.getCustomerById(id);

        return "customer id  :"+customer;
    }


    @PostMapping("/customer")
    public Customer saveCustomer(@RequestBody Customer customer){
        System.out.println(customer);

        //customerService.saveCustomer(customer);
        //  customerService.save(customer);
        customer.setName("zc0111");
        customerService.saveCustomer(customer); //0111

        customer.setName("zc0408");
        customerService.saveOrUpdate(customer); //0408
        return  customer;

    }
}
